package com.leetcodehot.problems;

import java.util.ArrayList;
import java.util.List;

public class problems78 {
    /**
     * 当前的元素 选与不选
     */
    /*private List<List<Integer>> res;

    public List<List<Integer>> subsets(int[] nums) {
        res = new ArrayList<List<Integer>>();
        List<Integer> temp = new ArrayList<>();
        int n = nums.length;
        dfs(nums, 0, n, temp);
        return res;
    }

    private void dfs(int[] nums, int i, int n, List<Integer> temp) {
        if (i == n) {
            res.add(new ArrayList<>(temp));
            return;
        }
        //不选
        dfs(nums, i + 1, n, temp);

        //选
        temp.add(nums[i]);
        dfs(nums, i + 1, n, temp);
        temp.remove(temp.size() - 1);
    }
    */

    /**
     * 从答案出发，第一个选哪个？第二个选哪个 发现下标要求顺序 且每轮都是结果
     */
    private List<List<Integer>> res;
    public List<List<Integer>> subsets(int[] nums) {
        res = new ArrayList<List<Integer>>();
        List<Integer> temp = new ArrayList<>();
        int n = nums.length;
        dfs(nums, 0, n, temp);
        return res;
    }

    private void dfs(int[] nums, int i, int n, List<Integer> temp) {
        //每一轮都是结果
        res.add(new ArrayList<>(temp));

        for(int j = i; j < n; j++) {
            temp.add(nums[j]);
            dfs(nums, j + 1, n, temp);
            temp.remove(temp.size() - 1);
        }
    }
}
